﻿using System.Linq;
using Csla.Data;

namespace Accounting.Library
{
    public class NhaCungCapService
    {
        /// <summary>
        /// Ghi tang cong no khach hang, nha cung cap
        /// </summary>
        /// <param name="ctx">Data access context</param>
        /// <param name="NhaCungCapId">Ma nha cung cap</param>
        /// <param name="SoTien">So tien ghi tang</param>
        public static void GhiTangCongNo(ContextManager<Accounting.DataAccess.AccountingDataContext> ctx, int NhaCungCapId, double SoTien) 
        {
            Accounting.DataAccess.DM_NhaCungCap nhaCungCap = (from p in ctx.DataContext.DM_NhaCungCaps
                                                                where (p.Khoa == NhaCungCapId)
                                                                select p).Single();

            double dinhMucCongNo = nhaCungCap.DinhMucCongNo == null ? 0 : (double) nhaCungCap.DinhMucCongNo;
            double dinhMucCongNoHienTai = nhaCungCap.DinhMucCongNoDaDung == null ? 0 : (double)nhaCungCap.DinhMucCongNoDaDung;

            double dinhMucCongNoDaDung = dinhMucCongNoHienTai +  SoTien;
            if (dinhMucCongNoDaDung > dinhMucCongNo)
            {
                throw new CapNhatDinhMucCongNoException(dinhMucCongNoHienTai, SoTien, LyDoCapNhat.GhiTang);
            }
            else
            {
                nhaCungCap.DinhMucCongNoDaDung = dinhMucCongNoDaDung;
                ctx.DataContext.SubmitChanges();
            }
        }

        /// <summary>
        /// Ghi giam cong no khach hang, nha cung cap
        /// </summary>
        /// <param name="ctx">Data access context</param>
        /// <param name="NhaCungCapId">Ma nha cung cap</param>
        /// <param name="SoTien">So tien ghi giam</param>
        public static void GhiGiamCongNo(ContextManager<Accounting.DataAccess.AccountingDataContext> ctx, int NhaCungCapId, double SoTien)
        {
            Accounting.DataAccess.DM_NhaCungCap nhaCungCap = (from p in ctx.DataContext.DM_NhaCungCaps
                                                              where (p.Khoa == NhaCungCapId)
                                                              select p).Single();

            double dinhMucCongNoHienTai = nhaCungCap.DinhMucCongNoDaDung == null ? 0 : (double)nhaCungCap.DinhMucCongNoDaDung;

            double dinhMucCongNoDaDung = dinhMucCongNoHienTai - SoTien;
            dinhMucCongNoDaDung = dinhMucCongNoDaDung < 0 ? 0 : dinhMucCongNoDaDung;
            nhaCungCap.DinhMucCongNoDaDung = dinhMucCongNoDaDung;
            ctx.DataContext.SubmitChanges();
        }

        /// <summary>
        /// Ghi tang ky gui khach hang, nha cung cap
        /// </summary>
        /// <param name="ctx">Data access context</param>
        /// <param name="NhaCungCapId">Ma nha cung cap</param>
        /// <param name="SoTien">So tien ghi tang</param>
        public static void GhiTangKyGui(ContextManager<Accounting.DataAccess.AccountingDataContext> ctx, int NhaCungCapId, double SoTien)
        {
            Accounting.DataAccess.DM_NhaCungCap nhaCungCap = (from p in ctx.DataContext.DM_NhaCungCaps
                                                              where (p.Khoa == NhaCungCapId)
                                                              select p).Single();

            double dinhMucKyGui = nhaCungCap.DinhMucKyGui == null ? 0 : (double)nhaCungCap.DinhMucKyGui;
            double dinhMucKyGuiHienTai = nhaCungCap.DinhMucKyGuiDaDung == null ? 0 : (double)nhaCungCap.DinhMucKyGuiDaDung;

            double dinhMucKyGuiDaDung = dinhMucKyGuiHienTai + SoTien;
            if (dinhMucKyGuiDaDung > dinhMucKyGui)
            {
                throw new CapNhatDinhMucKyGuiException(dinhMucKyGuiHienTai, SoTien, LyDoCapNhat.GhiTang);
            }
            else
            {
                nhaCungCap.DinhMucKyGuiDaDung = dinhMucKyGuiDaDung;
                ctx.DataContext.SubmitChanges();
            }
        }

        /// <summary>
        /// Ghi giam ky gui khach hang, nha cung cap
        /// </summary>
        /// <param name="ctx">Data access context</param>
        /// <param name="NhaCungCapId">Ma nha cung cap</param>
        /// <param name="SoTien">So tien ghi giam</param>
        public static void GhiGiamKyGui(ContextManager<Accounting.DataAccess.AccountingDataContext> ctx, int NhaCungCapId, double SoTien)
        {
            Accounting.DataAccess.DM_NhaCungCap nhaCungCap = (from p in ctx.DataContext.DM_NhaCungCaps
                                                              where (p.Khoa == NhaCungCapId)
                                                              select p).Single();

            double dinhMucKyGuiHienTai = nhaCungCap.DinhMucKyGuiDaDung == null ? 0 : (double)nhaCungCap.DinhMucKyGuiDaDung;

            double dinhMucKyGuiDaDung = dinhMucKyGuiHienTai - SoTien;
            dinhMucKyGuiDaDung = dinhMucKyGuiDaDung < 0 ? 0 : dinhMucKyGuiDaDung;
            nhaCungCap.DinhMucKyGuiDaDung = dinhMucKyGuiDaDung;
            ctx.DataContext.SubmitChanges();
        }
    }
}
